package siruna.WE.core.events;

import java.util.HashMap;

import org.apache.http.MethodNotSupportedException;

import siruna.WE.core.Constants;
import siruna.WE.core.Subject;
import siruna.WE.core.Widget;
import siruna.WE.core.Constants.InputTypes;
import siruna.WE.core.model.Block;
import siruna.WE.core.model.EventHandler;
import siruna.WE.core.model.EventList;
import siruna.WE.core.model.FunctionalBlock;
import siruna.WE.core.model.VisualBlock;
import siruna.WE.core.model.FunctionalBlocks.ImageLoaderImpl;
import siruna.WE.core.model.container.PageImpl;

public class EventListener {

	private Widget widget;
	private Subject subject;

	public void EventOccured(final Event e) {
		// Event Afhandelen
		System.out.println("Event Afhandelen: " + e.getMessage()
				+ "\neventlistid: " + e.getEventlistid());
		System.out.println("e.getEventlistid(): " + e.getEventlistid());
		System.out.println("e.getSourceid(): " + e.getSourceid());

		e.getEventlistid();

		System.out.println("widget: " + widget);
		System.out.println("widget.getEventlists(): " + widget.getEventlists());
		System.out.println("e.getEventlistid(): " + e.getEventlistid());
		System.out.println("widget.getEventlists().get(e.getEventlistid()): "
				+ widget.getEventlists().get(e.getEventlistid()));

		final EventList el = widget.getEventlists().get(e.getEventlistid());

		System.out.println("eventlist: " + el);

		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub

				for (EventHandler eh : el.getEventHandlers()) {
					VisualBlock b = widget.getVisblocks().get(
							eh.getAttributes().get("DestinationId"));

					// attributeevent voorlopig de enige !
					if (eh.getAttributes().get("DestinationValue") != null) {

						System.out.println("if");
						b.setAttribute(eh.getAttributes().get(
								"DestinationAttribute"), eh.getAttributes()
								.get("DestinationValue"));
					}

					// dataEvent
					// voorlopig alleen een source die een FunctionalBlock is
					else {
						System.out.println("else");

						for (String s : widget.getFunctblocks().keySet()) {
							System.out.println("functblocks: " + s);

						}

						System.out.println("Hans: " + eh);

						for (String s : eh.getAttributes().keySet()) {
							System.out.println("Hans:s: "
									+ eh.getAttributes().get(s));

						}

						FunctionalBlock f;
						if (eh.getAttributes().get("SourceId") != null) {

							f = widget.getFunctblocks().get(
									eh.getAttributes().get("SourceId"));
						}
						// FunctionalBlock

						else {
							f = widget.getFunctblocks().get(e.getSourceid());
						}

						InputTypes t = b.getType(eh.getAttributes().get(
								"DestinationAttribute"));

						switch (t) {
						case bytearray:

							try {
								b.setInput(eh.getAttributes().get(
										"DestinationAttribute"), f
										.toByteArray());
							} catch (MethodNotSupportedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;

						case list:

							try {

								System.out.println("EventListener: list"
										+ f.getId());

								b.setInput(eh.getAttributes().get(
										"DestinationAttribute"), f.toList());
							} catch (MethodNotSupportedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;
							
						case string:

							try {
System.out.println("f: "+f);
								b.setAttribute(eh.getAttributes().get(
								"DestinationAttribute"), f.toString());

								b.setInput(eh.getAttributes().get(
										"DestinationAttribute"), f.toList());
							} catch (MethodNotSupportedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;

						}
					}
					subject.updateVisualBlock(widget.getWidgetID(), b.getId());

				}
			}
		});

		t.start();
	}

	public void setWidget(Widget w) {
		this.widget = w;

	}

	public void setSubject(Subject subject) {
		this.subject = subject;

	}

}
